<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use DB;

//导入模型类 Userss
use App\Model\Userss;

use  Hash; //导入Hash的加密类
use App\Http\Requests\UsersInsertRequest;

//导入自定义类
use A;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 获取搜索的参数 提取单个参数 
        $k = $request->input("keyword");
        $kk = $request->input("keywords");
        // echo $k;
        // 列表
        // 分页
        $data = Userss::where('username','like','%'.$k.'%')->where('email','like','%'.$kk.'%')->paginate(2);
        
        return view('Admin.Users.index',['data'=>$data,'request'=>$request->all()]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
         return view("Admin.Users.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // //获取参数
        // $data = $request->all();
        // 删除不要的参数 获取除了某个参数之外的参数

        $data = $request->except(['repassword','_token']);

        // 加密密码
        $data['password'] = Hash::make($data['password']);
        // 将post未加添加的数据设成默认值
        $data['status'] = 1;
        //str_random 随机数 
        $data['token']=str_random(50);
        
        // dd($data);

        //换为模型的写法 create 模型的添加方法
        // 将获取的参数加入数据库的users表
        if(Userss::create($data)) {
            // with 设置success 
            return redirect('adminusers')->with("success",'添加成功');
        }else {
            return back()->with("error",'添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //调用关联方法
        $info = Userss::find($id)->info;
        // dd($info);
        
        return view('Admin.Users.info',['info'=>$info]);
    }

    //获取会员的收货地址
    public function address($id) {
        $address = Userss::find($id)->address;
        // dd($address);
        
        return view('Admin.Users.address',['address'=>$address]);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Userss::find($id);
       return view('Admin.Users.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $data = $request->except(['_method','_token']);
        //修改数据库里的数据
        if( USerss::where('id','=',$id)->update($data)) {
            // with 设置success 
            return redirect('adminusers')->with("success",'修改成功');
        }else {
            return back()->with("error",'修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除数据库里的数据
        if(Userss::where('id','=',$id)->delete()) {
            // with 设置success 
            return redirect('adminusers')->with("success",'删除成功');
        }else {
            return back()->with("error",'删除失败');
        }
    }

    //调用自定义函数
    public function a() {
        pay();
    }


    //调用自定义类
    public function b() {
        // 实例化
        $class = new A();

        $class->sendphone();
    }

    //短信验证
    public function c() {
        sendphone(13192864235);
    }

    //支付宝
    public function d() {
        pays(time(),"商品","0.01","商品支付");
    }

    //支付宝
    public function returnurl() {
        echo '支付成功';
    }
}
